package cn.smbms.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.smbms.pojo.User;
import cn.smbms.service.user.UserService;

@Controller
@RequestMapping("/pressuser")
public class UserPressController {
	@Resource
	private UserService userService;

	@RequestMapping("/login.html")
	public String sLogin() {
		return "sucess/login";
	}

	// 登录接口
	@RequestMapping("/suplogin")
	public String supUserLogin(String loginName, String loginPwd,
			HttpServletRequest request, HttpSession session) {
		User user = userService.getUser(loginName);
		if (user != null) {
			if (user.getPassword().equals(loginPwd)) {
				if (user.getRoleId() == 2) {
					session.setAttribute("sUser", user);
					return "sucess/index";
				} else {
					throw new RuntimeException("访问权限不足！");
				}
			} else {
				throw new RuntimeException("密码输入错误!");
			}
		} else {
			throw new RuntimeException("用户名不存在!");
		}
	}

	// 异常配置
	@ExceptionHandler(value = { RuntimeException.class })
	public String handlerException(RuntimeException e,
			HttpServletRequest request) {
		request.setAttribute("error", e);
		return "sucess/login";
	}

}
